// pages/staff/coupon.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    isIphoneX: '',
    sid:'',
    mid:'',
    total: 0,
    money: 0,
    amount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let modelmes = wx.getStorageSync('modelmes');
    let isIphoneX = app.globalData.isIphoneX;
    let user = wx.getStorageSync('user');
    this.setData({
      isIphoneX: isIphoneX,
      amount: options.amount,
      sid: user.id,
      mid: user.merchantid
    })
    this.getList();
  },

  decnum: function(e){
    let index = e.currentTarget.dataset.index, list = this.data.list;
    var num = list[index].num;
    if(num > 0){
      num--;
    }
    list[index].num = num;
    console.log(num);
    this.setData({
      list:list
    });
    this.count();
  },

  addnum: function(e){
    let index = e.currentTarget.dataset.index, list = this.data.list;
    var num = list[index].num;
    num++;
    list[index].num = num;
    this.setData({
      list:list
    });
    this.count();
  },

  count: function(){
    let list = this.data.list;
    var money = 0, total = 0;
    for(var i = 0; i < list.length; i++){
      money += list[i].num * list[i].amount;
      total += list[i].num;
    }
    this.setData({
      money: money,
      total: total
    });
  },

  giveout: function(){
    let total = this.data.total, list = this.data.list, coupondata = [];
    if(total == 0){
      wx.showToast({
        title: '请先选择优惠券',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    for(var i = 0; i < list.length; i++){
      var arr = {};
      arr['id'] = list[i].id;
      arr['num'] = list[i].num;
      if(arr['num'] != 0){
        coupondata.push(arr);
      }
    }
    app.util.request({
      url: 'entry/wxapp/entryTicket',
      data: {
          sid: this.data.sid,
          mid: this.data.mid,
          amount: this.data.amount,
          total: this.data.total,
          money: this.data.money,
          coupondata: coupondata,
          type: 2
      },
      cachetime: 30,
      success: function (res) {
        if(res.data.errno == 0){
          var tid = res.data.data;
          wx.redirectTo({
            url: '/pages/staff/giveout?tid='+tid,
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
  },

  getList: function(){
    var that = this;
    app.util.request({
      url: 'entry/wxapp/getCouponList',
      data: {
         mid:that.data.mid,
      },
      cachetime: 0,
      success: function (res) {
        if(res.data.errno == 0){
          that.setData({
            list: res.data.data.list
          });
          wx.stopPullDownRefresh()
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})